package com.aletto.general_store.data;

import com.aletto.general_store.data.model.Address;
import com.aletto.general_store.data.model.AuctionPageGoods;
import com.aletto.general_store.data.model.ConsumerOrder;
import com.aletto.general_store.data.model.Goods;
import com.aletto.general_store.data.model.GoodsDetails;
import com.aletto.general_store.data.model.OrderCompany;
import com.aletto.general_store.data.model.OrderCreate;
import com.aletto.general_store.data.model.OrderDetails;
import com.aletto.general_store.data.model.OrderNumber;
import com.aletto.general_store.data.model.OrderScore;
import com.aletto.general_store.data.model.PageResponse;
import com.aletto.general_store.data.model.Response;
import com.aletto.general_store.data.model.ServiceOrder;
import com.aletto.general_store.data.model.Store;
import com.aletto.general_store.data.model.StoreFavorites;
import com.aletto.general_store.data.model.StoreOrder;
import com.aletto.general_store.data.model.Upload;
import com.aletto.general_store.data.model.UserDetails;
import com.aletto.general_store.data.network.CommonParameterInterceptor;

import java.io.File;
import java.util.List;

import io.reactivex.rxjava3.core.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

import java.util.concurrent.TimeUnit;

public class RemoteDataSource implements IDataSource {

    private final WebServiceApi mWebServiceApi;

    RemoteDataSource() {

        mWebServiceApi = initialize().create(WebServiceApi.class);
    }

    private Retrofit initialize() {
        HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor();
        logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(12, TimeUnit.SECONDS)
                .addInterceptor(new CommonParameterInterceptor())
                .addNetworkInterceptor(logInterceptor)
                .build();

        return new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl("http://167.179.90.6:88")
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

//    @Override
//    public Observable<BasicResponse<UploadImageResponse>> upload(File file) {
//        Map<String, RequestBody> fileMap = new HashMap<>();
//        fileMap.put("file" + "\";filename=\"" + file.getName(),
//                RequestBody.create(file, MediaType.parse("image/png")));
//
//        return mWebServiceApi.upload(fileMap);
//    }

    @Override
    public Observable<Response<Upload>> upload(File file) {
        RequestBody requestFile = RequestBody.Companion.create(file, MediaType.Companion.parse("multipart/form-data"));
        MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
        return mWebServiceApi.upload(part);
    }

    @Override
    public Observable<Response<Store>> getShopDetails(String id, int status) {
        if (status == 0) {
            return mWebServiceApi.getShopDetails(id, null);
        }

        return mWebServiceApi.getShopDetails(id, status);
    }

    @Override
    public Observable<Response<PageResponse<Goods>>> getGoodsByShopId(
            int page, int size, String shopId, int type, int orderType) {

        if (orderType == 0 && type == 0) {
            return mWebServiceApi.homeGoods(page, size, null, null, null, shopId);

        } else if (orderType == 0) {
            return mWebServiceApi.homeGoods(page, size, null, type, null, shopId);

        } else if (type == 0) {
            return mWebServiceApi.homeGoods(page, size, null, null, orderType, shopId);
        }

        return mWebServiceApi.homeGoods(page, size, null, type, orderType, shopId);
    }

    @Override
    public Observable<Response<PageResponse<Goods>>> homeGoods(int page, int size) {

        return mWebServiceApi.homeGoods(page, size, null, null, null, null);
    }

    @Override
    public Observable<Response<GoodsDetails>> getGoodsDetails(String id, int status) {
        if (status == 0) {
            return mWebServiceApi.getGoodsDetails(id, null);
        }

        return mWebServiceApi.getGoodsDetails(id, status);
    }

    @Override
    public Observable<Response<UserDetails>> login(String address) {
        return mWebServiceApi.login(address);
    }

    @Override
    public Observable<Response<UserDetails>> userDetails() {
        return mWebServiceApi.getUserInfos();
    }

    @Override
    public Observable<Response<Object>> updateUserDetails(String id, String name, String head_img, String sign) {
        return mWebServiceApi.upUserInfo(id, name, head_img, sign);
    }

    @Override
    public Observable<Response<List<String>>> hotSearch() {
        return mWebServiceApi.hotSearch().map(MapUtils::hot_search);
    }

    @Override
    public Observable<Response<PageResponse<Goods>>> searchGoods(int page, int size, String keyword) {
        return mWebServiceApi.homeGoods(page, size, keyword, null, null, null);
    }

    @Override
    public Observable<Response<PageResponse<Store>>> storeLists(int page, int size, String keyword) {
        return mWebServiceApi.storeLists(page, size, keyword);
    }

    @Override
    public Observable<Response<List<String>>> searchPrompt(int type, String keyword) {
        return mWebServiceApi.searchPrompt(type, keyword).map(MapUtils::search_prompt);
    }

    @Override
    public Observable<Response<List<Address>>> addressList() {
        return mWebServiceApi.addressList();
    }

    @Override
    public Observable<Response<Object>> addAddress(String name, String phone, String address) {
        return mWebServiceApi.addAddress(name, phone, address);
    }

    @Override
    public Observable<Response<Object>> deleteAddress(int id) {
        return mWebServiceApi.deleteAddress(id);
    }

    @Override
    public Observable<Response<Object>> defaultAddress(int id) {
        return mWebServiceApi.defaultAddress(id);
    }

    @Override
    public Observable<Response<PageResponse<Goods>>> history(int page, int size) {
        return mWebServiceApi.myClickList(page, size);
    }

    @Override
    public Observable<Response<Object>> clearHistory(String id) {

        if (id == null) {
            return mWebServiceApi.delClickLog(null);
        }

        return mWebServiceApi.delClickLog(id);
    }

    @Override
    public Observable<Response<OrderNumber>> myOrderInfo() {
        return mWebServiceApi.myOrderInfo();
    }

    @Override
    public Observable<Response<PageResponse<ConsumerOrder>>> myOrderList(
            int page, int size, int type, String keyword) {

        if (type == 0) {
            return mWebServiceApi.myOrderList(page, size, null, keyword);
        }

        return mWebServiceApi.myOrderList(page, size, type, keyword);
    }

    @Override
    public Observable<Response<PageResponse<StoreOrder>>> shopOrderList(int page, int size, int type, String keyword) {
        return mWebServiceApi.shopOrderList(page, size, type, keyword);
    }

    @Override
    public Observable<Response<AuctionPageGoods>> myGoodsList(int page, int size, Integer status, String keyword) {
        return mWebServiceApi.myGoodsList(page, size, status, keyword);
    }

    @Override
    public Observable<Response<Object>> favoritesGoods(String id, boolean isFavorites) {
        return mWebServiceApi.likeGoods(id, isFavorites ? 1 : 2); //  1收藏, 2取消收藏
    }

    @Override
    public Observable<Response<PageResponse<Goods>>> favoritesGoodsList(int page, int size) {
        return mWebServiceApi.favoritesGoodsList(page, size, 1);
    }

    @Override
    public Observable<Response<Object>> favoritesStore(String id, boolean isFavorites) {
        return mWebServiceApi.likeShop(id, isFavorites ? 1 : 2); //  1收藏, 2取消收藏
    }

    @Override
    public Observable<Response<PageResponse<StoreFavorites>>> favoritesStoreList(int page, int size) {
        return mWebServiceApi.favoritesStoreList(page, size, 2);
    }

    @Override
    public Observable<Response<Object>> clearFavorites(int type) {
        return mWebServiceApi.delLikeLog(null, type);
    }


    @Override
    public Observable<Response<OrderCreate>> createOrder(String goods_id, int address_id, int num) {
        return mWebServiceApi.createOrder(goods_id, address_id, num);
    }

    @Override
    public Observable<Response<Object>> payOrder(Long order_id, String pay_network, String pay_hash) {
        return mWebServiceApi.payOrder(order_id, pay_network, pay_hash);
    }

    @Override
    public Observable<Response<OrderDetails>> orderDetails(String id) {
        return mWebServiceApi.getOrderDetails(id);
    }

    @Override
    public Observable<Response<Object>> upGoodsStatus(String id, int status) {
        return mWebServiceApi.upGoodsStatus(id, status);
    }

    @Override
    public Observable<Response<PageResponse<OrderScore>>> getGoodsScoreList(String goods_id, int page, int size) {
        return mWebServiceApi.getGoodsScoreList(goods_id, page, size);
    }

    @Override
    public Observable<Response<Object>> addGoods(
            int type,
            String name,
            String goods_img,
            String label,
            String desc,
            float price,
            int total_stock,
            String notice) {

        return mWebServiceApi.addGoods(type, name, goods_img, label, desc, null, price, total_stock, notice, 2);
    }

    @Override
    public Observable<Response<Object>> updateGoods(
            String id,
            int type,
            String name,
            String goods_img,
            String label,
            String desc,
            float price,
            int total_stock,
            String notice) {

        return mWebServiceApi.updateGoods(id, type, name, goods_img, label, desc, null, price, total_stock, notice, 2);
    }

    @Override
    public Observable<Response<Object>> upGoodsPrice(String id, float price) {
        return mWebServiceApi.upGoodsPrice(id, price);
    }

    @Override
    public Observable<Response<Object>> upGoodsStock(String id, int stock_type, int num) {
        return mWebServiceApi.upGoodsStock(id, stock_type, num);
    }

    @Override
    public Observable<Response<Object>> delGoods(String id) {
        return mWebServiceApi.delGoods(id);
    }

    @Override
    public Observable<Response<Object>> sendOrder(Long order_id, String send_id, String send_code) {
        return mWebServiceApi.sendOrder(order_id, send_id, send_code);
    }

    @Override
    public Observable<Response<List<OrderCompany>>> logisticsList() {
        return mWebServiceApi.logisticsList();
    }

    @Override
    public Observable<Response<PageResponse<ServiceOrder>>> kefuOrderList(int page, int size, int type, String keyword) {
        return mWebServiceApi.kefuOrderList(page, size, type, keyword);
    }

    @Override
    public Observable<Response<Object>> confirmReceipt(Long order_id) {
        return mWebServiceApi.confirmReceipt(order_id);
    }

    @Override
    public Observable<Response<Object>> orderRating(Long order_id, Integer score) {
        return mWebServiceApi.orderRating(order_id, score);
    }

}
